package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.qo.OrderChartQueryObject;
import cn.wolfcode.wms.qo.SaleChartQueryObject;
import cn.wolfcode.wms.service.BrandService;
import cn.wolfcode.wms.service.ChartsService;
import cn.wolfcode.wms.service.ClientService;
import cn.wolfcode.wms.service.SupplierService;
import cn.wolfcode.wms.util.RequiredPermission;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/chart")
public class ChartsController {
    @Autowired
    private ChartsService chartsService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private ClientService clientService;

    @RequestMapping("/order")
    @RequiredPermission("订货报表")
    public String orderChart(Map map, OrderChartQueryObject qo) {
        map.put("groupTypes", OrderChartQueryObject.groupTypes);
        map.put("suppliers", supplierService.selectAll());
        map.put("brands", brandService.selectAll());
        map.put("qo", qo);
        map.put("datas", chartsService.orderChart(qo));
        return "charts/order";
    }

    @RequestMapping("/sale")
    @RequiredPermission("销售报表")
    public String saleChart(Map map, SaleChartQueryObject qo) {
        map.put("groupTypes", SaleChartQueryObject.groupTypes);
        map.put("clients", clientService.selectAll());
        map.put("brands", brandService.selectAll());
        map.put("qo", qo);
        map.put("datas", chartsService.saleChart(qo));
        return "charts/sale";
    }

    @RequestMapping("/saleChartByBar")
    @RequiredPermission("销售报表柱状图")
    public String saleChartByBar(Map map, SaleChartQueryObject qo) {
        List<Map<String, Object>> maps = chartsService.saleChart(qo);
        List<Object> groupTypes = new ArrayList<>();
        List<Object> totalAmounts = new ArrayList<>();
        for (Map<String, Object> objectMap : maps) {
            groupTypes.add(objectMap.get("groupType"));
            totalAmounts.add(objectMap.get("totalAmount"));
        }
        map.put("groupTypes", JSON.toJSONString(groupTypes));
        map.put("totalAmounts", totalAmounts);
        map.put("groupName", SaleChartQueryObject.groupTypes.get(qo.getGroupBy()));
        map.put("qo", qo);
        return "/charts/saleChartByBar";
    }

    @RequestMapping("/saleChartByPie")
    @RequiredPermission("销售报表bing状图")
    public String saleChartByPie(Map map, SaleChartQueryObject qo) {
        List<Map<String, Object>> maps = chartsService.saleChart(qo);
        map.put("groupName", SaleChartQueryObject.groupTypes.get(qo.getGroupBy()));
        List<Object> groupTypes = new ArrayList<>();
        List<Object> datas = new ArrayList<>();
        BigDecimal maxAmount = BigDecimal.ZERO;
        for (Map<String, Object> objectMap : maps) {
            BigDecimal amount = (BigDecimal) objectMap.get("totalAmount");
            if (amount.compareTo(maxAmount) > 0){
                maxAmount =amount;
            }
                groupTypes.add(objectMap.get("groupType"));
            Map<String, Object> data = new HashMap<>();
            data.put("name", objectMap.get("groupType"));
            data.put("value", amount);
            datas.add(data);
        }
        map.put("qo", qo);
        map.put("groupTypes", JSON.toJSONString(groupTypes));
        map.put("datas", JSON.toJSONString(datas));
        map.put("maxAmount",maxAmount);
        return "/charts/saleChartByPie";
    }
}
